package com.asen.libcommon.util.xpopup

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.ProgressBar
import androidx.annotation.ColorRes
import androidx.core.content.ContextCompat
import com.asen.libcommon.R
import com.asen.libcommon.base.BaseApplication
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.core.ImageViewerPopupView
import com.lxj.xpopup.interfaces.OnImageViewerLongPressListener
import com.lxj.xpopup.interfaces.OnSrcViewUpdateListener
import com.lxj.xpopup.interfaces.XPopupCallback
import com.lxj.xpopup.interfaces.XPopupImageLoader
import com.lxj.xpopup.photoview.PhotoView
import java.io.File

/**
 * @date   : 2021/12/22
 * @author : asenLiang
 * @e-mail : 375427684@qq.com
 * @desc   :
 */

/**
 * CenterPopupView快速实现展示方法\
 *
 * @param isDismiss false：只有调用dismiss方法才可以关闭 true：背景和返回键都能关闭
 */
fun BasePopupView.extQuickShow(
    isDismiss: Boolean = true,
    view: View? = null,
    autoOpenSoftInput: Boolean? = false,
    callback: XPopupCallback? = null
): BasePopupView {
    XPopup.Builder(context).apply {
        atView(view)
        dismissOnBackPressed(isDismiss)
        dismissOnTouchOutside(isDismiss)
        asCustom(this@extQuickShow)
        autoOpenSoftInput(autoOpenSoftInput)
        setPopupCallback(callback)
        show()
    }
    return this
}



/**
 * 快递关闭弹窗
 */
fun BasePopupView.extQuickDismiss() {
    if (isShow) {
        dismiss()
    }
}

/**
 * 大图浏览类型弹窗，多张图片使用场景
 */
fun XPopup.Builder.extImageViewer(
    srcView: ImageView?,
    currentPosition: Int = 0,
    url: String,
    imageLoader: XPopupImageLoader = XPopupImageSimpleLoader()
): ImageViewerPopupView {
    return extImageViewer(
        srcView = srcView,
        currentPosition = currentPosition,
        urls = mutableListOf(url),
        imageLoader = imageLoader
    )
}

/**
 * 大图浏览类型弹窗，多张图片使用场景
 *
 * @param srcView               源View，就是你点击的那个ImageView，弹窗消失的时候需回到该位置。如果实在没有这个View，可以传空，但是动画变的非常僵硬，适用于在Webview点击场景
 * @param currentPosition       指定显示图片的位置
 * @param urls                  图片url集合
 * @param isInfinite            是否需要无限滚动，默认为false
 * @param isShowPlaceHolder     是否显示默认的占位View，默认为false
 * @param placeholderColor      占位View的填充色，默认为-1
 * @param placeholderStroke     占位View的边框色，默认为-1
 * @param placeholderRadius     占位View的圆角大小，默认为-1
 * @param isShowSaveBtn         是否显示保存按钮，默认显示
 * @param srcViewUpdateListener 当滑动ViewPager切换图片后，需要更新srcView，此时会执行该回调，你需要调用updateSrcView方法。
 * @param longPressListener     当图片长按的时候执行
 * @return
 */
fun XPopup.Builder.extImageViewer(
    srcView: ImageView?,
    currentPosition: Int = 0,
    urls: MutableList<Any>,
    imageLoader: XPopupImageLoader = XPopupImageSimpleLoader(),
    isInfinite: Boolean = false,
    isShowPlaceHolder: Boolean = false,
    placeholderColor: Int = -1,
    placeholderStroke: Int = -1,
    placeholderRadius: Int = -1,
    isShowSaveBtn: Boolean = false,
    @ColorRes bgColor: Int = R.color.gray_666666,
    srcViewUpdateListener: OnSrcViewUpdateListener? = null,
    longPressListener: OnImageViewerLongPressListener? = null
): ImageViewerPopupView {
    return this.asImageViewer(
        srcView,
        currentPosition,
        urls.toList(),
        isInfinite,
        isShowPlaceHolder,
        placeholderColor,
        placeholderStroke,
        placeholderRadius,
        isShowSaveBtn,
        ContextCompat.getColor(BaseApplication.application, bgColor),
        srcViewUpdateListener,
        imageLoader,
        longPressListener
    )
}

open class XPopupImageSimpleLoader : XPopupImageLoader {

    override fun loadSnapshot(uri: Any, snapshot: PhotoView, srcView: ImageView?) {

    }

    override fun loadImage(
        position: Int,
        uri: Any,
        popupView: ImageViewerPopupView,
        snapshot: PhotoView,
        progressBar: ProgressBar
    ): View {
        val imageView = PhotoView(popupView.context)
        loadImage(position, uri, imageView)
        return imageView
    }

    open fun loadImage(position: Int, uri: Any, imageView: ImageView) {
        //必须指定Target.SIZE_ORIGINAL，否则无法拿到原图，就无法享用天衣无缝的动画
        Glide.with(imageView).load(uri).apply(
            RequestOptions().placeholder(R.mipmap.icon_multiple_empty).override(Target.SIZE_ORIGINAL)
        ).into(imageView)
    }

    override fun getImageFile(context: Context, uri: Any): File? {
        try {
            return Glide.with(context).downloadOnly().load(uri).submit().get()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }

}